#include "mysql_package.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "common_log.h"

void* Mysql_Connect(const char *host, const int port, const char *user, const char *passwd) {
	MYSQL *handle = NULL;
	char reconnOpt = 0x01;

	handle = mysql_init(NULL);
	if (NULL == handle) {
		LOGERROR("Mysql_Connect()=%p,{%d,%s}\n", handle, mysql_errno(handle), mysql_error(handle));
		return NULL;
	}
	else {
		if (NULL == mysql_real_connect(handle, host, user, passwd, NULL, port, NULL, CLIENT_MULTI_STATEMENTS)) {	
			LOGERROR("mysql_real_connect(%p,%s,%s,%s,%d)=NULL,{%d,%s}\n", handle, host, user, passwd, port, mysql_errno(handle), mysql_error(handle));
			mysql_close(handle);
			return NULL;
		} 
		else {
			mysql_options(handle, MYSQL_OPT_RECONNECT, (char*)&reconnOpt); 
			return handle;
		}
	}
}

void Mysql_Disconnect(const void *handle) {
	mysql_close((MYSQL *)handle);
}

bool Mysql_QueryViolation(const void *handle, t_MysqlResult *sqlResult) {
	MYSQL *sqlHandle = (MYSQL *)handle;
	MYSQL_RES *sqlRes = NULL;
	MYSQL_ROW sqlRow;
	char sentence[] = "SELECT "
		"t_violation.c_EVENTTIMEMILLIS, "
		"t_violation.c_VENUE, "
		"t_violation.c_VEHICLEINDEX, "		
		"t_violation.c_VEHICLELICENSE, "
		"t_violation.c_VEHICLETYPE, "
		"t_violation.c_VIOLATIONTWO, "
		"t_violation.c_VIOLATIONONE, "
		"t_violation.c_EVIDENCEPICTURENAMES, "
		"t_violation.c_belt_driver_score, "
		"t_violation.c_belt_passenger_score, "
		"t_violation.c_phone_driver_score, "
		"t_violation.c_smoke_driver_score "		
		"FROM "
		"t_violation "
		"WHERE "
		"t_violation.c_PROCESSSTATUS NOT IN (8, 9) AND "
		"t_violation.c_VIOLATIONONE IN (1001, 1003, 1004) "		
		"ORDER BY "
		"t_violation.c_EVENTTIMEMILLIS ASC "
		"LIMIT 0, 1";	
	int queryRet;
	int result = false;

	mysql_query(sqlHandle, "SET NAMES GBK");
	queryRet = mysql_query(sqlHandle, "USE car_phone_server");
	if (0 != queryRet) {
		LOGERROR("mysql_query()=%d,{%d,%s}\n", queryRet, mysql_errno(sqlHandle), mysql_error(sqlHandle));
	}
	else {
		if (0 == (int)time(NULL)%60)
			LOGINFO("%s\r\n", sentence);
		queryRet = mysql_real_query(sqlHandle, sentence, strlen(sentence));
		if (0 != queryRet) {
			LOGERROR("mysql_real_query()=%d,{%d,%s}\n", queryRet, mysql_errno(sqlHandle), mysql_error(sqlHandle));
		}
		else {
			sqlRes = mysql_store_result(sqlHandle);
			sqlRow = mysql_fetch_row(sqlRes);
			if (NULL != sqlRow) {
				memset(sqlResult, 0, sizeof(t_MysqlResult));           
				strcpy(sqlResult->m_time, sqlRow[0]);
				strcpy(sqlResult->m_location, sqlRow[1]);
				strcpy(sqlResult->m_randValue, sqlRow[2]);					
				strcpy(sqlResult->m_license, sqlRow[3]);	
				strcpy(sqlResult->m_cartypeCode, sqlRow[4]);
				strcpy(sqlResult->m_carCode, sqlRow[5]);
				strcpy(sqlResult->m_vioCode, sqlRow[6]);	
				strcpy(sqlResult->m_picPath, sqlRow[7]);	
				sqlResult->m_belt_driver_score = atoi(sqlRow[8]);
				sqlResult->m_belt_passenger_score = atoi(sqlRow[9]);
				sqlResult->m_phone_driver_score = atoi(sqlRow[10]);
				sqlResult->m_smoke_driver_score = atoi(sqlRow[11]);				
				result = true;				
			}
			mysql_free_result(sqlRes);
		}
	}
	return result;	
}

bool Mysql_UpdateViolation(const void *handle, const t_MysqlResult *mysqlResult) {
	MYSQL *sqlHandle = (MYSQL *)handle;
	MYSQL_RES *sqlRes = NULL;
	MYSQL_ROW sqlRow;
	char sentence[1024];
	char sentencetmp[] = "UPDATE " 
		"t_violation "
		"SET "
		"t_violation.c_PROCESSSTATUS = 8 "
		"WHERE "
		"t_violation.c_EVENTTIMEMILLIS = %s AND "
		"t_violation.c_VEHICLEINDEX = %s AND "
		"t_violation.c_VEHICLELICENSE = '%s'";
	int queryRet;

	mysql_query(sqlHandle, "SET NAMES GBK");
	queryRet = mysql_query(sqlHandle, "USE car_phone_server");
	if (0 != queryRet) {
		LOGERROR("mysql_query()=%d,{%d,%s}\n", queryRet, mysql_errno(sqlHandle), mysql_error(sqlHandle));
		return false;
	}
	else {
		sprintf(sentence, sentencetmp, mysqlResult->m_time, mysqlResult->m_randValue, mysqlResult->m_license);
		if (0 == (int)time(NULL)%60)
			LOGINFO("%s\r\n", sentence);
		queryRet = mysql_real_query(sqlHandle, sentence, strlen(sentence));
		if (0 != queryRet) {
			LOGERROR("mysql_real_query()=%d,{%d,%s}\n", queryRet, mysql_errno(sqlHandle), mysql_error(sqlHandle));
			return false;
		}
		else {
			return true;
		}
	}
}


